package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.baisi.ClockRecordBaiSiDTO;
import com.xhwl.centiotdata.pojo.dto.baisi.StaffSchedulingDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IClockRecordCentService;
import com.xhwl.centiotdata.suppliersystem.baisi.ClockBaiSiHandler;
import com.xhwl.common.dto.cent.pedes.ClockRecordDTO;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: yanyixiong
 * @Description: 百思
 * @Date: create in 14:27 2022/1/4
 */
@Slf4j
@Service
public class PedestrianIntegrationBaiSiHandler extends PedestrianIntegrationHandler {

    @Autowired
    ClockBaiSiHandler clockBaiSiHandler;
    @Autowired
    IClockRecordCentService clockRecordCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(14, this);
    }

    @Override
    public void pullClockRecord(PedestrianIntegrationConfigDTO configDTO,  Integer offSet, Integer limit) {
        LocalDateTime now = LocalDateTime.now();
        int hour = now.getHour();
        //当每天2点前，额外拉取昨天的记录
        if(hour <= 2){
            offSet = 1;
            limit = 1;
        }
        //拉取数据
        List<ClockRecordBaiSiDTO> recordList = clockBaiSiHandler.pullClockRecord(configDTO, offSet,limit);
        log.error("获取的百思原始数据{}",recordList);
        List<StaffSchedulingDTO> staffList = clockBaiSiHandler.pullSchedulingStaff(configDTO, offSet,limit);
        //转换数据
        List<ClockRecordDTO> clockRecordDTOList = handlerRecord(mergeClockRecord(recordList, staffList), configDTO);
        //存储业务中台
        clockRecordCentService.saveBatch(clockRecordDTOList);
    }

    private List<ClockRecordBaiSiDTO> mergeClockRecord(List<ClockRecordBaiSiDTO> recordList,List<StaffSchedulingDTO> staffList){
        String clockStatus = "notClock";
        Map<String, ClockRecordBaiSiDTO> clockRecordBaiSiMap = staffList.stream()
                .filter(staffSchedulingDTO -> !StringUtil.isNullOrEmpty(staffSchedulingDTO.getStaffAccount()))
                .map(staffSchedulingDTO -> {
            ClockRecordBaiSiDTO clockRecordBaiSiDTO = new ClockRecordBaiSiDTO();
            clockRecordBaiSiDTO.setStaffAccount(staffSchedulingDTO.getStaffAccount());
            clockRecordBaiSiDTO.setBlStoreId(staffSchedulingDTO.getBlStoreId());
            clockRecordBaiSiDTO.setClockDate(staffSchedulingDTO.getClassesDate());
            clockRecordBaiSiDTO.setClockStatus(clockStatus);
            return clockRecordBaiSiDTO;
        }).collect(Collectors.toMap(clockRecordBaiSiDTO-> clockRecordBaiSiDTO.getStaffAccount()+clockRecordBaiSiDTO.getClockDate(), clockRecordBaiSiDTO -> clockRecordBaiSiDTO));
        recordList.forEach(clockRecordBaiSiDTO -> {
            clockRecordBaiSiMap.put(clockRecordBaiSiDTO.getStaffAccount()+clockRecordBaiSiDTO.getClockDate(),clockRecordBaiSiDTO);
        });
        return new ArrayList<>(clockRecordBaiSiMap.values());
    }

    /**
     *
     * @param recordList
     * @param configDTO
     * @return
     */
    private List<ClockRecordDTO> handlerRecord(List<ClockRecordBaiSiDTO> recordList,PedestrianIntegrationConfigDTO configDTO){
        List<ClockRecordDTO> clockRecordDTOList = new ArrayList<>(recordList.size());
        recordList.forEach(clockRecordBaiSiDTO -> {
            ClockRecordDTO clockRecordDTO = ClockRecordDTO.builder()
                    .clockDate(clockRecordBaiSiDTO.getClockDate())
                    .status(convertStatus(clockRecordBaiSiDTO.getClockStatus()))
                    .deptCode(clockRecordBaiSiDTO.getBlStoreId())
                    .userCode(clockRecordBaiSiDTO.getStaffAccount())
                    .projectId(configDTO.getProjectId()).build();
            clockRecordDTOList.add(clockRecordDTO);
        });
        return clockRecordDTOList;
    }

    /*
     * 杭州奥莱(5个状态)在、离岗状态优化:
     * 在岗：正常、迟到、早退
     * 离岗：缺卡、待核算
     * */
    private Integer convertStatus(String clockStatus){
        switch (clockStatus){
            case "Normal":
            case "Late":
            case "Early":
                return 0;
            default:
                return 1;
        }
    }


}
